package utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 工具类，生成验证码图片
 * 
 * @author huangyunjie
 *
 */
public class SecurityImage {

	/**
	 * 生成验证码图片
	 * 
	 * @param code
	 *            验证码字符
	 * @return BufferedImage 图片
	 */
	private static BufferedImage createImage(String code) {
		// 验证码长度
		int codeLength = code.length();
		// 字体大小
		int fSize = 25;
		int fWidth = fSize - 5;
		// 图片宽度
		int width = 90;
		// 图片高度
		int height = 34;
		// 图片
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = image.createGraphics();
		// 设置背景色
		graphics.setColor(Color.WHITE);
		// 填充背景
		graphics.fillRect(0, 0, width, height);
		// 设置边框颜色
		graphics.setColor(Color.LIGHT_GRAY);
		// 边框字体样式
		graphics.setFont(new Font("Arial", Font.BOLD, height - 2));
		// 绘制边框
		graphics.drawRect(0, 0, width - 1, height - 1);
		// 绘制噪点
		Random random = new Random();
		// 设置噪点颜色
		graphics.setColor(Color.RED);
		for (int i = 0; i < codeLength * 5; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			// 绘制1*1大小的矩形
			graphics.drawRect(x, y, 1, 1);
		}

		// 绘制验证码
		int codeY = height - 10;
		CreateRandomLine(width, height, 8, graphics, 100);
		// 设置字体颜色和样式
		graphics.setColor(Color.BLACK);
		graphics.setFont(new Font("Arial", Font.PLAIN, fSize));
		for (int i = 0; i < codeLength; i++) {
			graphics.drawString(String.valueOf(code.charAt(i)), i * 16 + 5, codeY);
		}
		// 关闭资源
		graphics.dispose();
		return image;
	}

	private static void CreateRandomLine(int width, int height, int minMany, Graphics g, int alpha) { // 随机产生干扰线条
		for (int i = 0; i < getIntRandom(minMany, minMany + 6); i++) {
			int x1 = getIntRandom(0, (int) (width * 0.6));
			int y1 = getIntRandom(0, (int) (height * 0.6));
			int x2 = getIntRandom((int) (width * 0.4), width);
			int y2 = getIntRandom((int) (height * 0.2), height);
			g.setColor(getColor(alpha));
			g.drawLine(x1, y1, x2, y2);
		}
	}

	private static int getIntRandom(int start, int end) {
		if (end < start) {
			int t = end;
			end = start;
			start = t;
		}
		int i = start + (int) (Math.random() * (end - start));
		return i;
	}

	private static Color getColor(int alpha) {
		int R = (int) (Math.random() * 255);
		int G = (int) (Math.random() * 255);
		int B = (int) (Math.random() * 255);
		return new Color(R, G, B, alpha);
	}

	/**
	 * 返回验证码图片的流格式
	 * 
	 * @param code
	 *            验证码
	 * @return ByteArrayInputStream 图片流
	 */
	public static ByteArrayInputStream getImageAsInputStream(String code) {
		BufferedImage image = createImage(code);
		return convertImageToStream(image);

	}

	/**
	 * 将BufferedImage转换成ByteArrayInputStream
	 * 
	 * @param image
	 *            图片
	 * @return ByteArrayInputStream 流
	 */
	private static ByteArrayInputStream convertImageToStream(BufferedImage image) {

		ByteArrayInputStream inputStream = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
		try {
			jpeg.encode(image);
			byte[] bts = bos.toByteArray();
			inputStream = new ByteArrayInputStream(bts);
		} catch (ImageFormatException e) {
			// TODO: handle exception
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return inputStream;

	}

}
